package com.newland.zxy;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.io.IOException;
import java.util.List;

/**
 * @author zhangxuyuan
 * @version 1.0
 * @date 2021/1/15
 */
public class ZkClientTestMain {

    private static ZkClient zkClient;
    public static void main(String[] args) throws IOException {
        zkClient=new ZkClient("127.0.0.1:2181");
        /**
         * 其他创建节点方法如下
         * zkClient.createEphemeralSequential();
         * zkClient.createEphemeral();
         * zkClient.createPersistentSequential()
         */
        //true表示递归创建
        zkClient.createPersistent("/zkclient/node",true);
        //写入数据
        zkClient.writeData("/zkclient/node","zkclient-node");
        //查询数据
        String o = zkClient.readData("/zkclient/node");
        System.out.println(o);
        //删除节点
//        zkClient.delete("/zkclient/node");
        //注册监听
        zkClient.subscribeChildChanges("/zkclient/node", new IZkChildListener() {
            public void handleChildChange(String s, List<String> list) throws Exception {
                System.out.println(s);
                System.out.println(list);
            }
        });
        System.in.read();
    }
}
